package com.awenhui.commonweb.validation.validator;

import com.awenhui.commonweb.validation.annotation.Phone;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 手机号码验证逻辑
 *
 * @author zxiaozhou
 * @date 2019-06-18 10:03
 * @since JDK1.8
 */
public class PhoneValidator implements ConstraintValidator<Phone, String> {
    @Override
    public void initialize(Phone constraintAnnotation) {
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        if (StringUtils.isBlank(value)) {
            return true;
        }
        String regexp = "^1[0-9]{10}$";
        return value.matches(regexp);
    }
}
